﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Assignment_10
{
    public partial class FrAdmin : Form
    {
        private static ConnectDB cDb;
        private static Boolean isInsert = false; // Check button Save action

        public FrAdmin()
        {
            InitializeComponent();
        }

        private void FrAdmin_Load(object sender, EventArgs e)
        {
            cDb = new ConnectDB();
            cDb.LoadDataFrAdmin(dgvAdmin);

            cDb.SetBindingSource(txtId, "Text", "EmpID");
            cDb.SetBindingSource(txtName, "Text", "EmpName");
            cDb.SetBindingSource(txtPass, "Text", "EmpPass");
            btnCancel.Enabled = false;

        }

        private void btnFirst_Click(object sender, EventArgs e)
        {
            cDb.GetBSource().MoveFirst();
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            cDb.GetBSource().MoveLast();
        }

        private void btnPre_Click(object sender, EventArgs e)
        {
            cDb.GetBSource().MovePrevious();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            cDb.GetBSource().MoveNext();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            cDb.GetBSource().AddNew();
            txtId.Focus();
            DisableControl();
            isInsert = true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            cDb.Cancel();
            isInsert = false;
            EnableCaption();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!ValidateForm())
            {
                return;
            }

            if (isInsert)
            {
                cDb.Update("Add");
            }

            else
            {
                cDb.Update("Update");
            }

            isInsert = false;
            EnableCaption();
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn chắc chắn muốn xóa dữ liệu này chứ?", "Admin", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                cDb.GetBSource().RemoveCurrent();
                cDb.Delete();
            }
        }

        private void txtId_TextChanged(object sender, EventArgs e)
        {
            //string id = dgvAdmin.SelectedRows[0].Cells[0].Value.ToString().Trim();
            //string name = dgvAdmin.SelectedRows[0].Cells[1].Value.ToString().Trim();
            //string pass = dgvAdmin.SelectedRows[0].Cells[2].Value.ToString().Trim();

            //if (id != txtId.Text.Trim() || name != txtName.Text.Trim() || pass != txtPass.Text.Trim())
            //{
            //    btnSave.Enabled = true;
            //}
            //else
            //{
            //    btnSave.Enabled = false;
            //}
        }

        private void dgvAdmin_CellClick(object sender, DataGridViewCellEventArgs e)
        {
        }

        private void dgvAdmin_SelectionChanged(object sender, EventArgs e)
        {
           
        }

        private void DisableControl()
        {
                dgvAdmin.Enabled = false;
                btnAdd.Enabled = false;
                btnDel.Enabled = false;
                btnFirst.Enabled = false;
                btnLast.Enabled = false;
                btnNext.Enabled = false;
                btnPre.Enabled = false;
                btnCancel.Enabled = true;
        }

        private void EnableCaption()
        {
                dgvAdmin.Enabled = true;
                btnAdd.Enabled = true;
                btnDel.Enabled = true;
                btnFirst.Enabled = true;
                btnLast.Enabled = true;
                btnNext.Enabled = true;
                btnPre.Enabled = true;
                btnCancel.Enabled = false;
        }

        private Boolean ValidateForm()
        {
            if (txtId.Text.Trim() == "")
            {
                lblCheckID.Visible = true;
                txtId.Focus();
                return false;
            }
            lblCheckID.Visible = false;

            if (txtName.Text.Trim() == "")
            {
                lblCheckName.Visible = true;
                txtName.Focus();
                return false;
            }
            lblCheckName.Visible = false;

            if (txtPass.Text.Trim() == "")
            {
                lblCheckPass.Visible = true;
                txtPass.Focus();
                return false;
            }
            lblCheckPass.Visible = false;

            return true;
        }
    }
}
